/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.garcomeletronico.dao.implementacao;

import br.com.garcomeletronico.bean.Mesa;
import br.com.garcomeletronico.dao.IDAOMesa;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Rivi
 */
public class DAOMesa extends DAOGenerico implements IDAOMesa {

    @Override
    public void inserir(Mesa pojo) throws SQLException {

        try {
            String sql = "insert into Mesa (andar, ambiente) "
                    + " values (?,?)";
            PreparedStatement stmt = this.getConexao().prepareStatement(sql);
            stmt.setString(1, pojo.getAndar());
            stmt.setString(2, pojo.getAmbiente());
            stmt.execute();
            stmt.close();
        } catch (SQLException e) {
            throw new SQLException(e.getMessage());
        }
    }

    @Override
    public void alterar(Mesa pojo) throws SQLException {
        try {
            String sql = ("update Mesa set andar= ?, ambiente= ? "
                    + "where codigo= ? ");
            PreparedStatement stmt = this.getConexao().prepareStatement(sql);
            stmt.setString(1, pojo.getAndar());
            stmt.setString(2, pojo.getAmbiente());
            stmt.setInt(3, pojo.getCodigo());
            stmt.executeUpdate();
            stmt.close();
        } catch (SQLException e) {
            throw new SQLException(e.getMessage());
        }
    }

    @Override
    public void excluir(Mesa pojo) throws SQLException {
        try {
            String sql = "delete from Mesa where codigo=?";
            PreparedStatement stmt = this.getConexao().prepareStatement(sql);
            stmt.setInt(1, pojo.getCodigo());
            stmt.execute();
            stmt.close();
        } catch (SQLException e) {
            throw new SQLException(e.getMessage());
        }
    }
    
        @Override
    public List<Mesa> listarTodos() throws SQLException {
        String sql = "select * from Mesa ";
        List<Mesa> minhaLista = null;
        PreparedStatement stmt;
        ResultSet rs;
        
        try {
            stmt = this.getConexao().prepareStatement(sql);
            rs = stmt.executeQuery();
            
            while (rs.next()) {
                if (minhaLista == null) {
                    minhaLista = new ArrayList<>();
                }
                
                Mesa c1 = new Mesa();
                c1.setCodigo(rs.getInt("codigo"));
                c1.setAndar(rs.getString("andar"));
                c1.setAmbiente(rs.getString("ambiente"));
                minhaLista.add(c1);
            }
            rs.close();
            stmt.close();
            
        } catch (SQLException e) {
            throw new SQLException(e.getMessage());
        }
        
        return minhaLista;
    }

    @Override
    public List<Mesa> listarPorNome(Mesa pojo) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.

    }

    @Override
    public Mesa listarPorId(Mesa pojo) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}
